package com.blockmeta.bbs.businesslibrary.util;

import android.net.Uri;
import android.text.TextUtils;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.provider.IApplicationProvider;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.OkHttpRequestBuilder;
import com.zhy.http.okhttp.builder.OtherRequestBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;

import okhttp3.MediaType;

import static com.blockmeta.bbs.baselibrary.utils.SecurityUtil.MD5;
import static com.zhy.http.okhttp.builder.OkHttpRequestBuilder.USER_AGENT;

public class OKHttpHelper {

    public static PostStringBuilder commonPostStringWithUid(String url) {
        PostStringBuilder requestBuilder = OkHttpUtils.postString().mediaType(MediaType.parse("application/json; charset=utf-8"));
        addCommonHeader(requestBuilder, url);
        return requestBuilder;
    }

    public static PostStringBuilder commonPostString(String url) {
        PostStringBuilder requestBuilder = OkHttpUtils.postString().mediaType(MediaType.parse("application/json; charset=utf-8"));
        addCommonHeaderWithUid(requestBuilder, url);
        return requestBuilder;
    }

    public static PostFormBuilder commonPostWithUid(String url) {
        PostFormBuilder requestBuilder = OkHttpUtils.post();
        addCommonHeaderWithUid(requestBuilder, url);
        return requestBuilder;
    }

    public static PostFormBuilder commonPost(String url) {
        PostFormBuilder requestBuilder = OkHttpUtils.post();
        addCommonHeader(requestBuilder, url);
        return requestBuilder;
    }

    public static OtherRequestBuilder commonPutWithUid(String url) {
        OtherRequestBuilder requestBuilder = OkHttpUtils.put();
        addCommonHeaderWithUid(requestBuilder, url);
        return requestBuilder;
    }

    public static OtherRequestBuilder commonPut(String url) {
        OtherRequestBuilder requestBuilder = OkHttpUtils.put();
        addCommonHeader(requestBuilder, url);
        return requestBuilder;
    }

    public static OtherRequestBuilder commonDeleteWithUid(String url) {
        OtherRequestBuilder requestBuilder = OkHttpUtils.delete();
        addCommonHeaderWithUid(requestBuilder, url);
        return requestBuilder;
    }

    public static OtherRequestBuilder commonDelete(String url) {
        OtherRequestBuilder requestBuilder = OkHttpUtils.delete();
        addCommonHeader(requestBuilder, url);
        return requestBuilder;
    }

    private static void addCommonHeader(OkHttpRequestBuilder requestBuilder, String url) {

        String time = String.valueOf((System.currentTimeMillis() / 1000));
        String token = SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN, "");
        Uri uri = Uri.parse(url);
        String subUrl = uri.getPath();
        String query = uri.getQuery();
        if (!TextUtils.isEmpty(query)) {
            subUrl = subUrl + "?" + query;
        }
        String sign = MD5(subUrl + token + time);
        IApplicationProvider provider = RouterHelper.getProvider(RouterPath.APPLICATION_PROVIDER);
        String myAppCode = AppCode.getMyAppCode(provider.getProjectBuildInfo().application_id);
        requestBuilder
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.TOKEN, ""))
                .addHeader(USER_AGENT, myAppCode)
                .addHeader("app_code", myAppCode)
                .addHeader("Authorization", GateWayUtil.getGateAuth())
                .addHeader("sign", sign)
                .addHeader("time", time);


    }

    private static void addCommonHeaderWithUid(OkHttpRequestBuilder requestBuilder, String url) {
        addCommonHeader(requestBuilder, url);
        requestBuilder
                .addHeader("uid", AccountHelper.getUid());
    }
}
